package com.neusoft.neuiotms.dcom.converter.hn;

import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.hn.HnInspectionHealthPlanDTO;
import com.neusoft.neuiotms.dcom.model.hn.HnInspectionHealthPlan;
import com.neusoft.neuiotms.dcom.repository.hn.HnInspectionHealthPlanRepository;

@Component
public class HnInspectionHealthPlanConverter
        extends AbstractConverter<HnInspectionHealthPlan, HnInspectionHealthPlanDTO> {

    @Autowired
    private HnInspectionHealthPlanRepository hnInspectionPlayRepository;
    @Autowired
    private HnInspectionItemConverter hnInspectionItemConverter;
    @Autowired
    private BaseIdGenerator hnInspectionHealthPlanCodeGenerator;

    @Override
    public HnInspectionHealthPlan toModel(final HnInspectionHealthPlanDTO dto) {
        HnInspectionHealthPlan model;
        if (dto.isNew()) {
            model = new HnInspectionHealthPlan();
        } else {
            model = this.hnInspectionPlayRepository.getOne(dto.getId());
        }
        model.setCode(StringUtils.isNotBlank(dto.getCode()) ? dto.getCode()
                : this.hnInspectionHealthPlanCodeGenerator.generateCode());
        model.setCronTime(dto.getCronTime());
        model.setDisabled(dto.isDisabled());
        model.setDuration(dto.getDuration());
        model.setUpdateTime(dto.getUpdateTime());
        model.getItems().clear();
        if (CollectionUtils.isNotEmpty(dto.getItems())) {
            model.setItems(this.hnInspectionItemConverter.toListModel(dto.getItems()));
        }
        model.setStartDateTime(dto.getStartDateTime());
        model.setEndDateTime(dto.getEndDateTime());

        model.setVersion(dto.getVersion());
        model.setDelFlag(dto.isDelFlag());
        model.setUserCode(dto.getUserCode());
        model.setUserName(dto.getUserName());
        model.setInstitutionCode(dto.getInstitutionCode());
        model.setInstitutionName(dto.getInstitutionName());
        return model;
    }

    @Override
    public HnInspectionHealthPlanDTO toDTO(final HnInspectionHealthPlan model, final boolean forListView) {
        final HnInspectionHealthPlanDTO dto = new HnInspectionHealthPlanDTO();
        dto.setCode(model.getCode());
        dto.setCreateTime(model.getCreateTime());
        dto.setCronTime(model.getCronTime());
        dto.setDisabled(model.isDisabled());
        dto.setDuration(model.getDuration());
        dto.setEndDateTime(model.getEndDateTime());
        dto.setId(model.getId());
        if (CollectionUtils.isNotEmpty(model.getItems())) {
            dto.setItems(this.hnInspectionItemConverter.toListDTO(model.getItems()));
        }
        dto.setStartDateTime(model.getStartDateTime());
        dto.setInstitutionCode(model.getInstitutionCode());
        dto.setInstitutionName(model.getInstitutionName());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setUserCode(model.getUserCode());
        dto.setUserName(model.getUserName());
        dto.setDelFlag(model.isDelFlag());
        dto.setVersion(model.getVersion());
        return dto;
    }

}
